﻿using SdmxMl.SmsImportCfg;
using System.Collections.Generic;
using System.Text;

namespace ChopPlugin
{
    /// <summary>
    /// Helper to manage permutation indication on all CHOP text (code text or annotation text)
    /// Rule: As the SDMX model does not offer a way to modelise it directly, we use the field
    /// URI in code and annotation url in annotation to persist the information.
    /// This is a logical information, it is presented as a chop url ended with the concerned languages 
    /// abbreviations. Ie: combination of D F R chars.
    /// </summary>
    public class Permutation
    {
        #region Constants

        /// <summary>
        /// Permutation info cannot be placed in SDMX model, so we use URI in code and in annotation 
        /// to place the information. the url is extended by the languages having permutation mark
        /// </summary>
        public const string PERMUTATION_MARK = "http://www.chop.ch/";
        const int indexPermutDE = 10;
        const int indexPermutFR = 13;
        const int indexPermutIT = 16;
        const char ITALIAN = 'I';
        const char GERMAN = 'D';
        const char FRENCH = 'F';

        #endregion

        #region Internal members

        private string Permutations;

        #endregion

        #region Constructor
        /// <summary>
        /// C'tor. Collects, in excel sheet row, permutation info for the different languages
        /// </summary>
        /// <param name="wh">Excel sheet accessor</param>
        /// <param name="row">row index to work with (start at 1)</param>
        public Permutation(WorkSheetHelper wh, int row)
        {
            StringBuilder sb = new StringBuilder();

            string permut = wh.GetCellContent(row, indexPermutDE);
            if (permut.Length > 0)
                sb.Append(GERMAN);
            permut = wh.GetCellContent(row, indexPermutFR);
            if (permut.Length > 0)
                sb.Append(FRENCH);
            permut = wh.GetCellContent(row, indexPermutIT);
            if (permut.Length > 0)
                sb.Append(ITALIAN);

            Permutations = sb.ToString();
        }
        #endregion

        #region Properties

        /// <summary> Returns marker to use to store permutation information. </summary>
        public string PermutationUri
        {
            get
            {
                return HasMark ? PERMUTATION_MARK + Permutations : string.Empty;
            }
        }
        /// <summary>True if permutation set for at least one language.</summary>
        public bool HasMark
        {
            get
            {
                return Permutations.Length > 0;
            }
        }
        #endregion

        #region Helper

        /// <summary>Check if submitted permutation marker is concerned by currently selected language</summary>
        /// <param name="uri">the uri string used to hold permutation info</param>
        /// <returns>True if permutation set for current language</returns>
        public static bool HasPermutationInCurrentLang(string uri)
        {
            string s = uri == null ? string.Empty : uri.Replace(PERMUTATION_MARK, "");
            List<char> list = new List<char>(s.ToCharArray());
            return list.Contains(SdmxMl.Common.LangSupport.CurLang.ToUpper()[0]);
        }
        #endregion
    }
}
